package com.atguigu.gmall.order.mq.config;


import com.atguigu.gmall.common.constant.RabbitConstant;
import org.springframework.amqp.core.*;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class OrderMqComponentConfiguration {


    @Bean
    public Exchange orderEventExchange() {
        TopicExchange topicExchange = new TopicExchange(RabbitConstant.EXCHANGE_ORDER_EVENT
                , true, false, null);
        return topicExchange;
    }

    @Bean
    public Queue orderDelayQueue() {
        Map<String, Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange", RabbitConstant.EXCHANGE_ORDER_EVENT);
        map.put("x-dead-letter-routing-key", RabbitConstant.RK_ORDER_TIMEOUT);
        map.put("x-message-ttl", 1000*60*30);
        Queue queue = new Queue(RabbitConstant.QUEUE_ORDER_DELAY,
                true,false,false,map);
        return queue;
    }

    @Bean
    public Binding  orderCreateBinding(Exchange orderEventExchange,Queue orderDelayQueue) {
        return BindingBuilder.bind(orderDelayQueue)
                .to(orderEventExchange)
                .with(RabbitConstant.RK_ORDER_CREATE).noargs();
    }

    @Bean
    public Queue orderDeadQueue() {
        Queue queue = new Queue(RabbitConstant.QUEUE_ORDER_DEAD,
                true,false,false,null);
        return queue;
    }

    @Bean
    public Binding  orderTimeoutBinding(Exchange orderEventExchange,Queue orderDeadQueue) {
        return BindingBuilder.bind(orderDeadQueue)
                .to(orderEventExchange)
                .with(RabbitConstant.RK_ORDER_TIMEOUT).noargs();
    }

    @Bean
    public Queue orderPayedQueue() {
        Queue queue = new Queue(RabbitConstant.QUEUE_ORDER_PAYED,
                true,false,false,null);
        return queue;
    }

    @Bean
    public Binding  orderPayedBinding(Exchange orderEventExchange,Queue orderPayedQueue) {
        return BindingBuilder.bind(orderPayedQueue)
                .to(orderEventExchange)
                .with(RabbitConstant.RK_ORDER_PAYED).noargs();
    }




}
